/*
   SC file interface
   Copyright (C) 2009-2010  Tomasz Borowik

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING3.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <cstdio>

#include "boost/filesystem.hpp"

#include <QtGui>

#include "window.h"

#include "Exception.h"

#include "AbstractFileFilter.h"
#include "AbstractFileSystemScanner.h"
#include "AbstractSourceFileReader.h"
#include "AbstractCycleFinderAlgorithm.h"

#include "FileInfo.h"

#include "FileSystemScanner.h"
#include "HeaderFileAnalyzer.h"
#include "HeaderFileFilter.h"
#include "SourceFileFilter.h"
#include "ORFilter.h"
#include "SourceFileReader.h"
#include "IterativeDFSMultipleCycleFinderAlgorithm.h"
#include "RecursiveDFSMultipleCycleFinderAlgorithm.h"

#include "Graph.h"
#include "GraphBuilder.h"

#include "Util.h"



Window::Window()
{
	resize (800, 600);
	
	pMenuFile = menuBar()->addMenu(tr("&File"));
	
	
	QAction* action = new QAction(tr("&Open..."), this);
	action->setShortcuts(QKeySequence::Open);
	action->setStatusTip(tr("Open the target directory"));
	
	connect(action, SIGNAL(triggered()), this, SLOT(FileOpen()));
	pMenuFile->addAction(action);
	
	pMenuFile->addSeparator();
	
	action = new QAction(tr("&Quit..."), this);
	action->setShortcuts(QKeySequence::Quit);
	
	connect(action, SIGNAL(triggered()), this, SLOT(close()));
	pMenuFile->addAction(action);
	
	
	pMenuHelp = menuBar()->addMenu(tr("&Help"));
	
	action = new QAction(tr("&About..."), this);
	
	connect(action, SIGNAL(triggered()), this, SLOT(About()));
	pMenuHelp->addAction(action);
	
	
	
	QWidget *top = new QWidget(this);
	QHBoxLayout *htlayout = new QHBoxLayout(top);
	
	htlayout->addWidget(new QLabel("Path: "));
	htlayout->addWidget(pPath = new QLineEdit("."));
	
	
	pTextBox = new QPlainTextEdit(this);
	pTextBox->setReadOnly(true);
	
	
	QWidget *bottom = new QWidget(this);
	QHBoxLayout *hlayout = new QHBoxLayout(bottom);
	
	pRecursive = new QCheckBox ("Recursive", this);
	hlayout->addWidget(pRecursive);
	
	pDebug = new QCheckBox ("Debug", this);
	hlayout->addWidget(pDebug);
	
	QPushButton *go = new QPushButton ("Go", this);
	hlayout->addWidget(go);
	
	connect(go, SIGNAL(clicked()), this, SLOT(Check()));
	
	
	QWidget *center = new QWidget(this);
	QVBoxLayout *layout = new QVBoxLayout(center);
	
	layout->addWidget(top);
	layout->addWidget(pTextBox);
	layout->addWidget(bottom);
	
	setCentralWidget (center);
	
	setWindowTitle(tr("[ZPR] Super-Magic-Awesome-Cycle-Finder"));
	
	
	show();
}

void	Window::FileOpen()
{
	
	QFileDialog::Options options;
	
	QString str = QFileDialog::getExistingDirectory(this,
				tr("Select Directory"),
				"",
				QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
	
	pPath->setText(str);
}

void	Window::About()
{
	QMessageBox::about (this, "About Cycle-Finder", "It finds cycles in source code...");
	
}

void	Window::Check()
{
	std::ostringstream	out;
	
	if (pDebug->checkState() == Qt::Checked)
		analyzer::setLogLevel(analyzer::DEBUG);
	else
		analyzer::setLogLevel(analyzer::INFO);
	
	analyzer::gpOut = &out;
	
	analyzer::HeaderFileAnalyzer analyzer;
	analyzer.setIncludeSubDirectories(pRecursive->checkState() == Qt::Checked);
	
	try {
		analyzer.scanDirectory(pPath->text().toStdString());
	}
	catch (boost::filesystem3::filesystem_error& e) {
		pTextBox->appendPlainText(QString(e.what()));
	}
	pTextBox->appendPlainText(QString::fromStdString(out.str()));
	pTextBox->appendPlainText("\n");
	
	analyzer::gpOut = &std::cout;
}
